package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.util.QiNiuUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * Created by lenovo on 2017/7/31.
 */
@Controller
@RequestMapping("/imgs")
public class FileUploadController {

  protected Logger logger = Logger.getLogger(FileUploadController.class);

  /*
  * 上传图片文件夹
  */
  private static final String UPLOAD_PATH = "upload/img/";

  /*
  * 上传图片
  */
  @RequestMapping(value = "/uploadImg")
  public void uploadImg(@RequestParam("upload") MultipartFile file,
      HttpServletRequest request,
      HttpServletResponse response) {
    try {
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
      String CKEditorFuncNum = request.getParameter("CKEditorFuncNum");
      String uploadContentType = file.getContentType();
      String expandedName = "";
      if (uploadContentType.equals("image/pjpeg")
          || uploadContentType.equals("image/jpeg")) {
        // IE6上传jpg图片的headimageContentType是image/pjpeg，而IE9以及火狐上传的jpg图片是image/jpeg
        expandedName = ".jpg";
      } else if (uploadContentType.equals("image/png")
          || uploadContentType.equals("image/x-png")) {
        // IE6上传的png图片的headimageContentType是"image/x-png"
        expandedName = ".png";
      } else if (uploadContentType.equals("image/gif")) {
        expandedName = ".gif";
      } else if (uploadContentType.equals("image/bmp")) {
        expandedName = ".bmp";
      } else {
        out.println("<script type=\"text/javascript\">");
        out.println("window.parent.CKEDITOR.tools.callFunction("
            + CKEditorFuncNum + ",'',"
            + "'文件格式不正确（必须为.jpg/.gif/.bmp/.png文件）');");
        out.println("</script>");
        return;
      }
      if (file.getSize() > 1024 * 1024 * 2) {
        out.println("<script type=\"text/javascript\">");
        out.println("window.parent.CKEDITOR.tools.callFunction("
            + CKEditorFuncNum + ",''," + "'文件大小不得大于2M');");
        out.println("</script>");
        return;
      }
      String url = QiNiuUtil.getUrl(file);
      // 返回"图像"选项卡并显示图片 request.getContextPath()为web项目名
      out.println("<script type=\"text/javascript\">");
      out.println("window.parent.CKEDITOR.tools.callFunction("
          + CKEditorFuncNum + ",'" + url
          + "','')");
      out.println("</script>");
      return;
    } catch (Exception e) {
      logger.error("富文本上传出错", e);
    }
  }
}